package com.linwen.excise.leetcode;

/**
 * @ClassName _543二叉树的直径
 * @Description TODO
 * @Author zero
 * @DATE 2024/4/28 3:34 AM
 * @Version 1.0
 */
public class _543二叉树的直径 {
    static int ans = 0;
    public static void main(String[] args) {
        int[] arr = {1,2};
        TreeNode treeNode = TreeUtil.buildTree(arr);
        System.out.println(diameterOfBinaryTree(treeNode));

    }

    public static int diameterOfBinaryTree(TreeNode root) {
        if(root == null){
            return 0;
        }
        findDepth(root);
        return ans;

    }

    public static int findDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        int left = findDepth(root.left);
        int right = findDepth(root.right);
        ans = Math.max(ans,left+right);
        return Math.max(left,right)+1;
    }

}
